package com.bjsxt.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjsxt.domain.AdminAddress;
import com.bjsxt.domain.Coin;
import com.bjsxt.mapper.AdminAddressMapper;
import com.bjsxt.service.AdminAddressService;
import com.bjsxt.service.CoinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminAddressServiceImpl extends ServiceImpl<AdminAddressMapper, AdminAddress> implements AdminAddressService {

    /**
     * 币种业务层对象
     */
    @Autowired
    private CoinService coinService;

    /**
     * 根据分页对象和 ID 查询钱包归集地址
     *
     * @param page 分页对象<AdminAddress>
     * @param id   平台归账手续费等账户 ID
     * @return 钱包归集地址分页数据集
     */
    @Override
    public Page<AdminAddress> pageById(Page<AdminAddress> page, Long id) {
        // 调用分页查询的方法进行数据的查询并返回
        return page(page, new LambdaQueryWrapper<AdminAddress>().eq(id != null, AdminAddress::getId, id));
    }

    /**
     * 添加一个钱包的归集地址
     *
     * @param adminAddress 钱包的归集地址数据
     * @return true 添加成功 false 添加失败
     */
    @Override
    public boolean addAdminAddress(AdminAddress adminAddress) {
        // 调用币种业务层进行数据的查询，需要查询出对应的币种类型
        Coin coin = coinService.getById(adminAddress.getCoinId());
        // 断言，如果数据为空则进行报错
        Assert.notNull(coin, "保存归集地址失败，coin 找不到");
        // 将查询到的币种类型设置在保存的数据中
        adminAddress.setCoinType(coin.getType());
        // 调用业务层进行数据的保存，并返回
        return save(adminAddress);
    }
}
